package cn.le.poster.draw;

import cn.le.poster.PosterElement;
import cn.le.poster.element.Graphical;
import cn.le.poster.enums.EnumShape;

import java.awt.*;

public class GraphicalDraw implements PosterDraw{

    @Override
    public boolean draw(Graphics2D graphics, PosterElement element) {
        if (element instanceof Graphical == false){
            return false;
        }
        Graphical graphical = (Graphical) element;

        int w = graphical.getSize().getWidth();
        int h = graphical.getSize().getHeight();
        int x = graphical.getPosition().getX();
        int y = graphical.getPosition().getY();

        Color color = graphical.getColor();
        if (EnumShape.RoundedRectangle.equals(graphical.getShape())){
            roundedRectangle(graphics,color,x,y,w,h,graphical.getArcW(),graphical.getArcH());
        }else if (EnumShape.None.equals(graphical.getShape())){
            noneDraw(graphics,color,x,y,w,h);
        }
        return true;
    }

    /**
     * 圆角矩形
     */
    public void roundedRectangle(Graphics2D graphics,Color color,int x,int y,int w,int h,int arcW,int arcH){
        graphics.setColor(color);
        graphics.fillRoundRect(x,y,w,h,arcW,arcH);
        graphics.setColor(color);
        graphics.setStroke(new BasicStroke(8f));  //设置画笔
    }

    public void noneDraw(Graphics2D graphics,Color color,int x,int y,int w,int h){
        graphics.setBackground(color);//设置背景色
        graphics.clearRect(x, y, w, h);
        graphics.setPaint(Color.BLUE);//设置画笔,设置Paint属性
    }

}
